package com.im.easy.rxeasyandroidHelper.module.welcome;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.im.easy.rxeasyandroidHelper.R;
import com.im.easy.rxeasyandroidHelper.common.ui.BaseActivity;
import com.im.easy.rxeasyandroidHelper.model.bean.welcome.WelcomeADInfo;
import com.im.easy.rxeasyandroidHelper.module.MainActivity;
import com.im.easy.rxeasyandroidHelper.module.welcome.presenter.WelcomeADContract;
import com.im.easy.rxeasyandroidHelper.module.welcome.presenter.WelcomeADPresenter;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

import java.util.concurrent.TimeUnit;

public class WelcomeADActivity extends BaseActivity<WelcomeADPresenter> implements WelcomeADContract.View {

    @BindView(R.id.tv_second)
    TextView tvSecond;
    @BindView(R.id.layout_skip)
    LinearLayout layoutSkip;
    @BindView(R.id.iv_advertising)
    ImageView ivAdvertising;

    /**
     * 倒计时dispose
     */
    private Disposable countdownD;


    @Override
    public void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initEventAndData() {
        getPresenter().getData();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_welcome_ad;
    }

    @Override
    public void callback(boolean success, final WelcomeADInfo info) {
        if (success) {
            ivAdvertising.setVisibility(View.VISIBLE);
            ImageView img = ivAdvertising;
            Glide.with(this).load(info.getImage()).into(img);
            countDown(info.getTime());
        } else {
            LogUtils.d("请求异常");
        }
    }


    private void countDown(final int count) {
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//ui线程中进行控件更新
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        countdownD = d;
                    }

                    @Override
                    public void onNext(Long num) {
                        tvSecond.setText(num.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        jumpTarget();
                    }
                });

    }


    private void countDownDispose() {
        if (countdownD != null) {
            countdownD.dispose();
        }
    }

    /**
     * destroy
     */
    @Override
    protected void onDestroy() {
        countDownDispose();
        super.onDestroy();
    }


    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_advertising, R.id.layout_skip})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_advertising:
                break;
            case R.id.layout_skip:
                jumpTarget();
                break;
        }
        countDownDispose();
    }

    /**
     * 跳转目标
     */
    private void jumpTarget(){
        startActivity(new Intent(WelcomeADActivity.this, MainActivity.class));
        WelcomeADActivity.this.finish();
    }
}
